<?php if (empty($_POST)) 
{ 
?>
	<form action="test.php" name="images" id="cont" method="post" enctype="multipart/form-data">
	dfadsfsadf
	</form>
<?php
}
elseif (isset($_FILES['src']))
{
	$template = RTemplate::getTemplate();
	$db = RClassDb::getInstance();
	
	$values = array();
	$error = array();
	$imagecount = count($_FILES['src']['name']);
	$content = '';
	
	include(BP.DS.'library'.DS.'image'.DS.'resizer.php');
	$image = new SimpleImage();
	
	foreach ($_FILES['src']['name'] as $cnt => $name)
	{
		if (is_uploaded_file($_FILES['src']['tmp_name'][$cnt]))
		{
			$name = substr(mysql_real_escape_string(translit($name)), 0, 150);
			$description = mysql_real_escape_string($_POST['txt'][$cnt]);
			$date = date('Y-m-d H:i:s');
			$image->load($_FILES['src']['tmp_name'][$cnt]);
			$image->save(BP.DS.'images'.DS.'advertimages'.DS.'original'.DS.$name);
			$image->resize(70, 50);
			$thumb = 'images'.DS.'advertimages'.DS.'thumbs'.DS.$name;
			$image->save(BP.DS.$thumb);
			$values[] = '("'.$name.'", "'.$description.'", "'.$date.'", "'.$date.'")';
			$content .= '<img src="/'.$thumb.'" />';
		}
		else
		{
			$error[] = $_FILES['src']['name'];
		}
	}
	
	$query = 'INSERT INTO #__advert_photos (src, image_description, created, modified) VALUES '.implode(', ', $values);
	$db->setValues($query);
	
	$endid = $db->getLastId();
	$imgid = '';
	for ($i = $endid - $imagecount + 1; $i <= $endid; $i++)
		$imgid .= '<input type="hidden" name="advert_photos_id[]" value="'.$i.'" />';
	
	$content = '
		<script>
			var span = window.opener.document.createElement("span");
			span.innerHTML += \''.$imgid.'\';
			window.opener.document.getElementById("images").appendChild(span);
		</script>
	';
	$template->registerVar('content', $content);

	unset($image);
}
?>